<template>
	<view class="page">
		<view class="address-input">
			<view class="list-input">
				<view class="title">
					<text>收货人</text>
				</view>
				<view class="content">
					<input v-model="form.name" type="text" placeholder="请填写收货人姓名">
				</view>
			</view>
			<view class="list-input">
				<view class="title">
					<text>手机号</text>
				</view>
				<view class="content">
					<input v-model="form.phone" type="tel" placeholder="请填写收货人手机号">
				</view>
			</view>
			<view class="list-input">
				<view class="title">
					<text>所在地区</text>
				</view>
				<el-cascader v-model="form.value" :options="options" @change="handleChange" placeholder="省市/区县/乡镇">
				</el-cascader>
			</view>
			<view class="list-textarea">
				<view class="title">
					<text>详细地址</text>
				</view>
				<view class="content">
					<textarea v-model="form.detailed_address" type="tel" placeholder="街道/楼牌号等" />
				</view>
			</view>
		</view>
		<view class="tag-default">
			<view class="tag-list">
				<view class="title">
					<text>标签</text>
				</view>
				<view class="content">
					<view class="list action">
						<text>家</text>
					</view>
					<view class="list">
						<text>公司</text>
					</view>
					<view class="list">
						<text>学校</text>
					</view>
				</view>
			</view>
			<view class="default-address">
				<view class="title">
					<text>默认地址</text>
				</view>
				<view class="switch-default">
					 <el-switch v-model="form.is_default"></el-switch>
				</view>
			</view>
		</view>
		<view class="footer-btn">
			<view class="btn">
				<text @click="save">保存</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				addressType: '2',
				options: [],
				form:{
					name:"",
					phone:"",
					detailed_address:"",
					is_default:false,
					value: [],
				}
			};
		},
		onLoad(params) {
			this.$http.get('city', {}).then(res => {
				this.options = res.data.data
			})
			
			this.addressType = params.type || '2';
			uni.setNavigationBarTitle({
				title: this.addressType === '1' ? '编辑收货地址' : '新建收货地址'
			})
			if(params.type == 1){
				var eid = params.eid
				this.$http.get('user/addressById',{
					eid:eid
				},{}).then(res => {
					this.form.name = res.data.data.name
					this.form.phone = res.data.data.phone
					this.form.detailed_address = res.data.data.detailed_address
					this.form.is_default = res.data.data.is_default
					var value = []
					value.push(res.data.data.province_region)
					value.push(res.data.data.city_region)
					value.push(res.data.data.district_region)
					this.form.value = value
				})
			}
			console.log(this.form)
		},
		methods: {
			handleChange(value) {
				console.log(value);
			},
			save() {
                var form = this.form
				console.log(form)
				this.$http.post('user/address', 
				form, 
				{}).then(res => {
					console.log(res)
					uni.showToast({
					    title: res.message,
					    duration: 2000
					});
					if(res.code == 200){
						uni.navigateTo({
							url: '/pages/AddressList/AddressList',
						})
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	@import 'AddressEdit.scss';
</style>
